﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Ports;

namespace NewTools
{
    internal class SerialPortUtil
    {
        private MainWindow mMainWindow = null;
        private static SerialPortUtil instance = null;

        public static SerialPortUtil GetInstance(MainWindow mainWindow)
        {
            if (instance == null)
                instance = new SerialPortUtil(mainWindow);
            return instance;
        }
        public SerialPortUtil(MainWindow mainWindow)
        {
            this.mMainWindow = mainWindow;
        }

        public StopBits SerialProtStopBits_Get(string info)
        {
            StopBits stopBits = StopBits.None;
            if (info.Equals("1"))
            {
                stopBits = StopBits.One;
            }
            else if (info.Equals("1.5"))
            {
                stopBits = StopBits.OnePointFive;
            }
            else if (info.Equals("2"))
            {
                stopBits = StopBits.Two;
            }
            return stopBits;
        }

        public Int32 SerialPortBaudRate_Get(string baudRate)
        {
            return Convert.ToInt32(baudRate);
        }

        public string SerialPortName_Get(string protName)
        { 
            return protName;
        }

        public Int32 SerialPortDataBits_Get(string dataBits)
        {
            return Convert.ToInt32(dataBits);
        }

        public Parity SerialPortParity_Get(string parityText)
        {
            Parity parity = Parity.None;

            if (parityText.Equals("None"))
            {
                parity = Parity.None;
            }
            else if (parityText.Equals("odd"))
            {
                parity = Parity.Odd;
            }
            else if (parityText.Equals("Even"))
            {
                parity = Parity.Even;
            }
            return parity;
        }

        public bool TvSerialPort_Check()
        {
            Console.WriteLine(mMainWindow.comboBox_serial_port.Text.Trim());
            if ((mMainWindow.comboBox_serial_port.Text.Trim() == ""))
            {
                return false;
            }
            return true;
        }

        public void TvSerialPortProperty_Set()
        {
            mMainWindow.SerialPortTv.PortName = SerialPortName_Get(mMainWindow.comboBox_serial_port.Text.Trim());
            mMainWindow.SerialPortTv.BaudRate = 115200;
            mMainWindow.SerialPortTv.DataBits = 8;
            mMainWindow.SerialPortTv.StopBits = StopBits.One;
            mMainWindow.SerialPortTv.Parity = Parity.None;
        }

        public void AvailableSerialPort_Get()
        {
            mMainWindow.comboBox_serial_port.Items.Clear();
            mMainWindow.comboBox_serial_port.Text = "";
            mMainWindow.ComboBoxSerialSignal.Items.Clear();
            mMainWindow.ComboBoxSerialSignal.Text = "";
            string[] portNames = SerialPort.GetPortNames();
            if (portNames.Length <= 0)
            {
                mMainWindow.comboBox_serial_port.SelectedIndex = -1;
                mMainWindow.ComboBoxSerialSignal.SelectedIndex = -1;
                mMainWindow.TvSTatusLabel.Text = Properties.Resources.TvSerialPortDisconnect;
            }
            else
            {
                mMainWindow.comboBox_serial_port.Items.AddRange(portNames);
                mMainWindow.ComboBoxSerialSignal.Items.AddRange(portNames);

                bool isHaveInIni = false;
                bool isTvPortInIni = false;
                string signalPotName = ParseIni.GetInstance().SignalSerialPortName_Read();
                foreach (string port in portNames)
                {
                    if (port.Equals(signalPotName))
                    {
                        isHaveInIni = true;
                    }
                }
                if (isHaveInIni)
                {
                    mMainWindow.ComboBoxSerialSignal.Text = signalPotName;
                }
                else
                {
                    mMainWindow.ComboBoxSerialSignal.SelectedIndex = 0;
                }
                string tvPortName = ParseIni.GetInstance().TVSerialPortName_Read();
                foreach (string port in portNames)
                {
                    if (port.Equals(tvPortName))
                    {
                        isTvPortInIni = true;
                    }
                }
                if (isTvPortInIni)
                {
                    mMainWindow.comboBox_serial_port.Text = tvPortName;
                }
                else
                {
                    mMainWindow.comboBox_serial_port.SelectedIndex = 0;
                }
                mMainWindow.TvSTatusLabel.Text = Properties.Resources.TvSerialPortDisconnect;
            }
        }
    }
}
